package hotel.base.oversea.utils;

import com.google.common.reflect.TypeToken;
import com.google.gson.*;
import hotel.base.oversea.vo.meituan.response.order.MtHotelOrderCheckVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author HuangT
 * @date 2020/7/2 15:56
 */
public class GsonUtils {

    private static Gson gson = null;

    static {
        if (gson == null) {
            gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .create();
        }
    }


    private GsonUtils() {
    }


    /**
     * 将object对象转成json字符串
     *
     * @param object
     * @return
     */
    public static String GsonString(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }


    /**
     * 将gsonString转成泛型bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 将obj转成泛型bean
     *
     * 内部会再转一次 GsonString，该方法看情况操作
     *
     * @param object
     * @param cls
     * @return
     */
    public static <T> T objToBean(Object object, Class<T> cls) {
        String s = GsonString(object);
        T t = null;
        if (gson != null) {
            t = gson.fromJson(s, cls);
        }
        return t;
    }



    /**
     * 转成list
     * 解决泛型在编译期类型被擦除导致报错
     *
     * @param json
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToList(String json, Class<T> cls) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }


    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
            }.getType());
        }
        return list;
    }


    /**
     * 非Integer类型转成map的
     *
     * @param gsonString
     * @return
     */
    public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }


    /**
     * <H1>Integer类型转map</H1>
     *
     * @param gsonString
     * @return java.util.Map<java.lang.String,java.lang.Integer>
     * @author HuangT
     * @date 2020/7/2 19:30
    */
    public static Map<String, Integer> IntegerToMaps(String gsonString) {
        Map<String, Integer> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, Integer>>() {
            }.getType());
        }
        return map;
    }

    public static void main(String[] args) {
        String str = "{\"code\":8,\"desc\":\"价格发生变化!\"," +
                "\"priceModels\":[{\"date\":\"2021-01-07\",\"salePrice\":10300,\"subPrice\":669,\"subRatio\":650,\"dayType\":0}],\"remainRoomNum\":999}";
        MtHotelOrderCheckVo mtHotelOrderCheckVo = GsonToBean(str, MtHotelOrderCheckVo.class);
        MtHotelOrderCheckVo parse = JacksonUtil.parse(str, MtHotelOrderCheckVo.class);
        System.out.println(parse);

    }


}
